<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1,shrink-to-fit=no">
    <title>首页</title>
    <link rel="icon" href="/img/login.jpg" type="img/x-ico" />
    <script src="/js/jquery.js"></script>
    <link href="/css/xcode.min.css" rel="stylesheet"/>
    <link href="/css/style.min.css" rel="stylesheet"/>
</head>


<body  gtools_scp_screen_capture_injected="true" >
<![endif]-->
<header id="header" class="header bg-white">
    <div class="navbar-container">
        <a class="navbar-logo">拉钩教育小站</a>
        <div class="navbar-menu">
            <form name="logoutform"  method="post"></form>
            <a class="header-info"  id="userInfo"></a>
            <a class="header-info" style="cursor: pointer;" onclick="goLogin()" >退出</a>
            <!--<a style="color: #fff" >订阅</a>-->
<!--            <a  class="header-info" href="/admin">后台管理</a>-->
        </div>
<!--        <div class="navbar-search" onclick="">-->
<!--            <span class="icon-search"></span>-->
<!--            <form role="search" onsubmit="return false;">-->
<!--                <span class="search-box">-->
<!--                    <input type="text" id="search-inp" class="input" placeholder="搜索..." maxlength="30"-->
<!--                           autocomplete="off"/>-->
<!--                </span>-->
<!--            </form>-->
<!--        </div>-->
    </div>
</header>
<div id="imgDiv">

    <img class="mb-4" src="/img/welcome.jpg" width="100%" height="96%">
</div>
</body>

<script>
    console.log('document.cookie6666 >>>>>>>>>>>>',document.cookie);
    let allHeight = window.screen.availHeight;
    document.getElementById("imgDiv").style.height = (allHeight - 80) + 'px';

    //获取cookie，即token
    let myCookie = getCookie("lagouSessionId");
    console.log('myCookie >>>>>>>>>>>>',myCookie);
    if(myCookie && myCookie != 0){
        getUserInfo(myCookie);
    }else{
        alert("未获取到用户信息");
        goLogin();
    }
    function getUserInfo(token) {
        $.ajax({
            url:"http://www.test.com/api/user/info/" + token,
            type:"GET" ,
            crossDomain: true,
            // 下面这句话允许跨域的cookie访问
            xhrFields: {
                withCredentials: true
            },
            success:function (data,status,xhr) {
                console.log('*********** 获取用户信息返回 **********data:',data);
                if(data.code == 0){
                    $("#userInfo").html(data.result + " ,欢迎小主登录！")
                }else{
                    alert(data.msg);
                    goLogin();
                }
            },
            error:function (err) {
                console.log(err);
                alert(err.responseText)
                goLogin();
            }
        });
    }

    function getCookie(cookieName){
        var cookieValue="";
        console.log('>>>>>>>>>coo:',document.cookie)
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = cookies[i];
                if (cookie.substring(0, cookieName.length).trim() == cookieName.trim()) {
                    cookieValue = cookie.substring(cookieName.length + 1, cookie.length);
                    break;
                }
            }
        }
        return cookieValue;
    };

    function goLogin() {
        clearAllCookie();
        $.ajax({
            url:"http://www.test.com/api/api/user/logout",
            type:"GET" ,
            crossDomain: true,
            // 下面这句话允许跨域的cookie访问
            xhrFields: {
                withCredentials: true
            },
            success:function (data,status,xhr) {
                console.log('*********** 退出登录返回 **********data:',data);
            },
            error:function (err) {
                console.log(err);
            }
        });
        window.location.href = "login.html"
    };

    //删除所有cookie
    function clearAllCookie() {
        var date=new Date();
        date.setTime(date.getTime()-10000);
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        console.log("需要删除的cookie名字："+keys);
        if (keys) {
            for (var i =  keys.length; i--;)
                document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";
        }
    }

</script>
<!--<body >-->
<!--&lt;!&ndash;<div style="background-image: url(../login/img/welcome.jpg);height: 100%;width: 110%;background-repeat: no-repeat;" >&ndash;&gt;-->
<!--<span>发手动阀dsf</span>-->
<!--</div>-->
<!--&lt;!&ndash;<img class="mb-4" src="../login/img/welcome.jpg" width="100%" height="105%">&ndash;&gt;-->

<!--</body>-->
<style>
    .navbar-container {
        width: 100%;
    }
</style>

</html>